package cn.kgc.springcloud.controller;

import cn.kgc.springcloud.entity.CollectionDTO;
import cn.kgc.springcloud.entity.Product;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

/**
 * @author: mosin
 * @version: v1.0  2022/6/9
 */
@RestController
@RequestMapping("product")
public class ProductController {
    private static final Logger logger = LoggerFactory.getLogger(ProductController.class);

    @RequestMapping("product1")
    public  String test01(HttpServletRequest request){
        // 程序进入阻塞的状态   演示openfeign请求超时
        //try {
        //    Thread.sleep(3000);
        //} catch (InterruptedException e) {
        //    e.printStackTrace();
        //}
        String username = request.getHeader("username");
        return "商品服务被调用,端口号是:"+request.getServerPort()+"username:"+username;
    }

    @RequestMapping("product2")
    public  String test02(HttpServletRequest request,@RequestParam Integer id){

        logger.info("订单请求发送的id:{}",id);

        return "商品服务被调用,端口号是:"+request.getServerPort();
    }

    @RequestMapping("product3/{id}")
    public  String test03(HttpServletRequest request,@PathVariable("id") Integer id){
        logger.info("订单请求发送的id:{}",id);
        return "商品服务被调用,端口号是:"+request.getServerPort();
    }

    @RequestMapping("product4")
    public  String test04(HttpServletRequest request, @RequestBody Product product){
        logger.info("订单请求发送的订单对象={}",product);
        return "商品服务被调用,端口号是:"+request.getServerPort();
    }


    @RequestMapping("product5")
    public  String test05(HttpServletRequest request,@RequestParam("ids") String[] ids){
        //遍历数组
        for (int i = 0; i < ids.length; i++) {
            logger.info("订单请求发送的数组值={}",ids[i]);
        }
        return "商品服务被调用,端口号是:"+request.getServerPort();
    }

    @RequestMapping("product6")
    public  String test06(HttpServletRequest request, @RequestBody CollectionDTO collectionDTO){
        ArrayList<Integer> ids = collectionDTO.getIds();
        //遍历数组
        for (int i = 0; i < ids.size(); i++) {
            logger.info("订单请求发送的数组值={}",ids.get(i));
        }
        return "商品服务被调用,端口号是:"+request.getServerPort();
    }

    @RequestMapping("product7")
    public Product test07(@RequestParam("id") Integer id){
        Product phone = new Product("phone", id);
        return phone;
    }

    @RequestMapping("product8")
    public ArrayList<Product> test08(){

        Product phone1 = new Product("phone1", 1);
        Product phone2 = new Product("phone2", 2);
        Product phone3 = new Product("phone3", 3);
        Product phone4 = new Product("phone4", 4);

        ArrayList<Product> products = new ArrayList<Product>();
        products.add(phone1);
        products.add(phone2);
        products.add(phone3);
        products.add(phone4);
        return products ;
    }

    @RequestMapping("product9")
    public Map<String,Object> test09(){

        HashMap<String, Object> map = new HashMap<String, Object>();

        Product phone1 = new Product("phone1", 1);
        Product phone2 = new Product("phone2", 2);
        Product phone3 = new Product("phone3", 3);
        Product phone4 = new Product("phone4", 4);
        ArrayList<Product> products = new ArrayList<Product>();
        products.add(phone1);
        products.add(phone2);
        products.add(phone3);
        products.add(phone4);

        map.put("count", 20);
        map.put("data", products);

        return map ;
    }

}
